import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com")

# 通过ID定位
# driver.find_element(By.CSS_SELECTOR, '#kw').send_keys('css')
# driver.find_element(By.CSS_SELECTOR, '#su').click()

# 通过类定位
# driver.find_element(By.CSS_SELECTOR, '.nav-search-input').send_keys('类选择器')

# 属性选择器
# driver.find_element(By.CSS_SELECTOR, '[id=kw]').send_keys('属性选择器')
# driver.find_element(By.CSS_SELECTOR, '[autocomplete="off"]').send_keys('属性选择器')

# 根据标签属性定位
# driver.find_element(By.CSS_SELECTOR, 'a[href="http://image.baidu.com/"]').click()
# 模糊匹配-包含*
# driver.find_element(By.CSS_SELECTOR, 'a[href*="image.baidu.com"]').click()
# 模糊匹配-匹配开头^
# driver.find_element(By.CSS_SELECTOR, 'a[href^="http://image.baidu"]').click()
# 模糊匹配-匹配结尾$
# driver.find_element(By.CSS_SELECTOR, 'a[href$="image.baidu.com/"]').click()
# 组合定位
# driver.find_element(By.CSS_SELECTOR, 'input.s_ipt').send_keys('哈哈哈')  # 类名为s_ipt的input标签
# driver.find_element(By.CSS_SELECTOR, 'input#kw').send_keys('哈哈哈')  # ID为s_ipt的input标签
# 定位子元素
# id为's-top-left'的元素下面的第一个a标签 (默认取第一条数据)
# driver.find_element(By.CSS_SELECTOR, '#s-top-left>a').click()
# driver.find_element(By.CSS_SELECTOR,'div.s-top-left-new.s-isindex-wrap>a').click()  # 当有多个class时可以多几个.   class="s-top-right s-isindex-wrap"
# id为's-top-left'的元素下面的第3个a元素
driver.find_element(By.CSS_SELECTOR, '#s-top-left>a:nth-child(3)').click()
time.sleep(3)
driver.close()